﻿using System.Windows.Forms;
using MobileCommissionManagement.Common;

namespace MobileCommissionManagement.Controls
{
    public partial class EntityTypeList : UserControl
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.EntityTypeSaved += delegate { SetGridDataSource(); };
                SetGridDataSource();
            }
        }

        #endregion

        public EntityTypeList()
        {
            InitializeComponent();            
            gridView.DoubleClick += GridViewDoubleClick;
            editToolStripMenuItem.Click += EditToolStripMenuItemClick;
            addToolStripMenuItem.Click += AddToolStripMenuItemClick;
            changeSituationToolStripMenuItem.Click += ChangeSituationToolStripMenuItemClick;
            contextMenuStrip.Opening += ContextMenuStripOpening;
        }

        #region Private Methods
       
        private void SetGridDataSource()
        {
            gridControl.DataSource = _controller.EntityTypes;
        }

        private static TiposEntidade GetClone(TiposEntidade entityType)
        {
            var document = new TiposEntidade
                               {
                                   IdTipoEntidade = entityType.IdTipoEntidade,
                                   Nome = entityType.Nome,
                                   Activo = entityType.Activo
                               };
            return document;
        }

        private static void Merge(TiposEntidade entityType, TiposEntidade document)
        {
            entityType.Nome = document.Nome;
            entityType.Activo = document.Activo;
        }

        private void ShowEntityType(TiposEntidade entityType)
        {
            TiposEntidade document = GetClone(entityType);
            var window = new EntityType {Document = document};
            if(DialogResult.OK != window.ShowDialog())
                return;
            Merge(entityType, window.Document);
            _controller.SaveEntityType(entityType);
        }
        
        #endregion  

        void GridViewDoubleClick(object sender, System.EventArgs e)
        {
            var entityType = (TiposEntidade)gridView.GetRow(gridView.FocusedRowHandle);
            if (entityType == null)
                return;
            ShowEntityType(entityType);
            _controller.SaveEntityType(entityType);
        }

        void EditToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            var entityType = (TiposEntidade)gridView.GetRow(gridView.FocusedRowHandle);
            ShowEntityType(entityType);
        }

        void AddToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            var entityType = new TiposEntidade();
            entityType.IdTipoEntidade = -1;
            entityType.Nome = string.Empty;
            entityType.Activo = true;
            ShowEntityType(entityType);
        }

        void ChangeSituationToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            var entityType = (TiposEntidade)gridView.GetRow(gridView.FocusedRowHandle);
            if (entityType == null)
                return;
            entityType.Activo = !entityType.Activo;
            _controller.SaveEntityType(entityType);
        }
        
        void ContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var entityType = (TiposEntidade)gridView.GetRow(gridView.FocusedRowHandle);
            editToolStripMenuItem.Enabled = entityType != null;
            changeSituationToolStripMenuItem.Enabled = entityType != null;
            removeToolStripMenuItem.Enabled = false;
        }

    }
}
